function Variables()
{
}

Variables.prototype.Initialize = function(XMLString)
 {
    var xmlDoc;
    this.WinIE = true;
    if (XMLString == "")
    {
        XMLString = "<?xml version='1.0' encoding='UTF-8'?><vars></vars>"
    }
    try
    //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(XMLString);
    }
    catch(e)
    {
        try
        //Firefox, Mozilla, Opera, etc.
        {
            parser = new DOMParser();
            xmlDoc = parser.parseFromString(XMLString, "text/xml");
            this.WinIE = false;
        }
        catch(e)
        {
            alert("Error:" + e.message);
            return;
        }
    }
    try
    //Internet Explorer
    {
        this.XMLDocument = xmlDoc;
        this.XVariable = this.XMLDocument.documentElement.childNodes;
    }
    catch(e)
    {
        alert("Error:" + e.message);
    }
}

Variables.prototype.length = function()
 {
    return this.XVariable.length;
}

Variables.prototype.getValue = function(cName)
 {
    for (var i = 0; i < this.XVariable.length; i++)
    {
        if (this.XVariable[i].nodeType == 1)
        {
            var _name = this.XVariable[i].getAttribute('n');
            if (cName == _name)
            {
                return this.XVariable[i].getAttribute('v');
            }
        }
    }
    return "";
}

Variables.prototype.setValue = function(cName, cValue)
 {
    for (var i = 0; i < this.XVariable.length; i++)
    {
        if (this.XVariable[i].nodeType == 1)
        {
            var _name = this.XVariable[i].getAttribute('n');
            if (cName == _name)
            {
                this.XVariable[i].setAttribute('v', cValue);
                return;
            }
        }
    }
    var AddNode = this.XMLDocument.createElement("var");
    AddNode.setAttribute('n', cName);
    AddNode.setAttribute('v', cValue);
    this.XMLDocument.documentElement.appendChild(AddNode);
    return;
}

Variables.prototype.Serialize = function(args)
 {
    var cXML = this.XMLDocument.xml;
    if (this.XMLDocument.xml == null)
    {
        try
        //Internet Explorer
        {
            var serializer = new XMLSerializer();
            cXML = serializer.serializeToString(this.XMLDocument);
        }
        catch(e)
        {
            alert("Serialize Error:" + e.message);
        }
    }
    return encodeURIComponent(cXML);
}
var GetNodeValue = function(obj)
 {
    var str = "";
    if (window.ActiveXObject)
    //IE
    {
        str = obj.text;
    }
    else
    //Mozilla
    {
        try
        {
            str = obj.childNodes[0].nodeValue;
        }
        catch(ex)
        {
            str = "";
        }
    }
    return str;
}

try
{

		if (document.implementation && document.implementation.createDocument)
		 {
		    XMLDocument.prototype.loadXML = function(xmlString)
		    {
		        var childNodes = this.childNodes;
		        for (var i = childNodes.length - 1; i >= 0; i--)
		        {
		        	this.removeChild(childNodes[i]);
		      	}
		
		        var dp = new DOMParser();
		        var newDOM = dp.parseFromString(xmlString, "text/xml");
		        var newElt = this.importNode(newDOM.documentElement, true);
		        this.appendChild(newElt);
		    };
		
		    // check for XPath implementation
		    if (document.implementation.hasFeature("XPath", "3.0"))
		    {
		        // prototying the XMLDocument
		        XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
		        {
		            if (!xNode) 
		            {
		                xNode = this;
		            }
		            var oNSResolver = this.createNSResolver(this.documentElement)
		            var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
		            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		            var aResult = [];
		            for (var i = 0; i < aItems.snapshotLength; i++)
		            {
		                aResult[i] = aItems.snapshotItem(i);
		            }
		            return aResult;
		        }
		
		        // prototying the Element
		        Element.prototype.selectNodes = function(cXPathString)
		        {
		            if (this.ownerDocument.selectNodes)
		            {
		                return this.ownerDocument.selectNodes(cXPathString, this);
		            }
		            else {
		                throw "For XML Elements Only";
		            }
		        }
		    }
		
		    // check for XPath implementation
		    if (document.implementation.hasFeature("XPath", "3.0"))
		    {
		        // prototying the XMLDocument
		        XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
		        {
		            if (!xNode) {
		                xNode = this;
		            }
		            var xItems = this.selectNodes(cXPathString, xNode);
		            if (xItems.length > 0)
		            {
		                return xItems[0];
		            }
		            else
		            {
		                return null;
		            }
		        }
		
		        // prototying the Element
		        Element.prototype.selectSingleNode = function(cXPathString)
		        {
		            if (this.ownerDocument.selectSingleNode)
		            {
		                return this.ownerDocument.selectSingleNode(cXPathString, this);
		            }
		            else {
		                throw "For XML Elements Only";
		            }
		        }
		    }
		}

}
catch(ex)
{
	
}

function XMLRecordset(oXMLDocument)
 {
    this.getColumns = _lst_col;
    this.getValue = _get_field;
    this.setValue = _set_field;
    this.RecordCount = _cnt_rec;
    this.ColumnCount = _cnt_col;
    this.MoveFirst = _mv_first;
    this.MovePrevious = _mv_prev;
    this.MoveNext = _mv_next;
    this.MoveLast = _mv_last;
    this.MoveLast = _mv_last;
    this.Tag = _set_tag;
    this.Delete = _del_rec;
    this.Checked = _checked;
    this.Unchecked = _unchecked;
    this.MoveTo = _mv_to;
    this.EOF = _eof;
    this.BOF = _bof;
    this.Serialize = _serialize;
    this.AbsolutePosition = _get_curr_rec;
    this.XMLDocument = oXMLDocument;

    this.Columns = this.XMLDocument.selectSingleNode("/wxf/columns").childNodes;
    if (this.XMLDocument.selectSingleNode("/wxf/rows"))
    {
        this.Records = this.XMLDocument.selectSingleNode("/wxf/rows").childNodes;
    }
    this.currentRecord = 0;
}

function _cnt_col()
 {
    return this.Columns.length;
}

function _cnt_rec()
 {
    return this.Records.length;
}

function _get_curr_rec()
 {
    return this.currentRecord;
}
function _eof()
 {

    }
function _bof()
 {

    }

function _mv_first()
 {
    this.currentRecord = 0;
}

function _mv_prev()
 {
    this.currentRecord = (this.currentRecord == 0) ? this.RecordCount() - 1: this.currentRecord - 1;
}

function _mv_next()
 {
    this.currentRecord = (this.currentRecord == this.RecordCount() - 1) ? 0: this.currentRecord + 1;
}

function _mv_last()
 {
    this.currentRecord = this.RecordCount() - 1;
}

function _mv_to(idx)
 {
    this.currentRecord = idx;
}

function _lst_col()
 {
    var lst = new Array();

    for (m = 0; m < this.Columns.length; m++)
    {
        var attrs = new Array();
        var _col_name = this.Columns[m].getAttribute('n');

        attrs['name'] = _col_name;
        attrs['type'] = this.Columns[m].getAttribute('t');
        attrs['status'] = this.Columns[m].getAttribute('status');
        attrs['ndx'] = m;

        lst[_col_name] = attrs;
    }
    return lst;
}

function _get_field(name)
 {
    var cols = this.getColumns();
    var idx = 0;
    if (IsNumeric(name))
    {
        idx = name * 1;
    } else {
        if (cols[name] == 'undefined')
        {
            return "";
        } else {
            idx = cols[name]['ndx'];
        }
    }
    var varDataRow = this.Records[this.currentRecord];
    if (varDataRow)
    {
        return varDataRow.childNodes[idx].text;
    } else {
        return null;
    }
}

function _set_field(name, newVal)
 {
    var cols = this.getColumns();
    var idx = 0;
    if (IsNumeric(name))
    {
        idx = name * 1;
    } else {
        for (i = 0; i < cols.length; i++)
        {
            if (cols[i]['name'] == name)
            {
                idx = i;
                break;
            }
        }
    }
    var varDataRow = this.Records[this.currentRecord];
    if (varDataRow)
    {
        varDataRow.childNodes[idx].text = newVal;
        varDataRow.setAttribute('s', 'UPDATE');
    }
}

function _set_tag()
 {
    var varDataRow = this.Records[this.currentRecord];
    if (varDataRow)
    {
        varDataRow.setAttribute('e', 'tag');
    }
}

function _checked()
 {
    var varDataRow = this.Records[this.currentRecord];
    if (varDataRow)
    {
        varDataRow.setAttribute('c', 'y');
    }
}

function _unchecked()
 {
    var varDataRow = this.Records[this.currentRecord];
    if (varDataRow)
    {
        varDataRow.setAttribute('c', 'n');
    }
}

function _del_rec()
 {
    var varDataRow = this.Records[this.currentRecord];
    if (varDataRow)
    {
        varDataRow.setAttribute('s', 'DELETE');
        varDataRow.setAttribute('e', 'DELETE');
    }
}

function _serialize()
 {
    var cXML = this.XMLDocument.xml;
    if (this.XMLDocument.xml == null)
    {
        try
        //Internet Explorer
        {
            var serializer = new XMLSerializer();
            cXML = serializer.serializeToString(this.XMLDocument);
        }
        catch(e)
        {
            alert("Serialize Error:" + e.message);
        }
    }
    return encodeURIComponent(cXML);
}
